﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using StockUtil;

namespace StockLib
{
    public class StockBlockGroupClass : StockBlockGroup
    {
        /// <summary>
        /// "分类"板块组名称
        /// </summary>
        public const string BLOCK_GROUP_NAME = "分类";

        /// <summary>
        /// "分类"板块组ID
        /// </summary>
        public const string BLOCK_GROUP_ID = BLOCK_GROUP_NAME + ".";

        /// <summary>
        /// 单例实例
        /// </summary>
        private static StockBlockGroupClass _instance = new StockBlockGroupClass();

        /// <summary>
        /// 
        /// </summary>
        private Dictionary<string, Func<Stock, bool>> classList = new Dictionary<string, Func<Stock, bool>>();

        /// <summary>
        /// 构造函数
        /// </summary>
        private StockBlockGroupClass()
            : base()
        {
            classList.Add("上证Ａ股", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^60")));
            classList.Add("上证Ｂ股", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^90")));
            classList.Add("深证Ａ股", r => (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^(000|002|300)")));
            classList.Add("深证Ｂ股", r => (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^200")));
            classList.Add("上证债券", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^(0|1)[0-9]") && !Regex.IsMatch(r.Code, @"^000")));
            classList.Add("深证债券", r => (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^1[0123]")));
            classList.Add("沪深Ａ股", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^60")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^(000|002|300)")));
            classList.Add("沪深Ｂ股", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^90")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^200")));
            classList.Add("沪深债券", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^(0|1)[0-9]") && !Regex.IsMatch(r.Code, @"^000")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^1[0123]")));
            classList.Add("沪深基金", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^5[012]")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^1[568]")));
            classList.Add("沪深权证", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^58")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^0[38]")));
            classList.Add("中小板", r => (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^002")));
            classList.Add("创业板", r => (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^300")));
            classList.Add("指数", r => (r.Market == StockMarket.SHSE && Regex.IsMatch(r.Code, @"^(000|1[ABC])")) || (r.Market == StockMarket.SZSE && Regex.IsMatch(r.Code, @"^39")));
        }

        /// <summary>
        /// 访问单例
        /// </summary>
        /// <returns></returns>
        public static StockBlockGroupClass Instance
        {
            get
            {
                return _instance;
            }
        }

        /// <summary>
        /// 加载"分类"板块组数据
        /// </summary>
        public override void Load()
        {
            this.Clear();

            foreach (var kvp in classList)
            {
                StockBlock block = new StockBlock();
                block.Name = kvp.Key;
                block.ShortName = StringHelper.GetPinYin(block.Name);
                block.GroupName = BLOCK_GROUP_NAME;
                this.Add(block.BlockId, block);

                var stocks = StockContainer.Instance.Values.Where(kvp.Value);
                foreach (Stock stock in stocks)
                {
                    block.Add(stock.CodeKey, stock);
                }
            }
        }

        /// <summary>
        /// 保存"分类"板块组数据
        /// </summary>
        public override void Save()
        {
        }
    }
}
